//
//  JSViewController45.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/6/18.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import Alamofire
import RxAlamofire
import RxCocoa
import RxSwift
import RxDataSources

class JSViewController45: BaseViewController {
    let disposeB = DisposeBag()
    var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        test7()
    }

}

extension JSViewController45 {
    fileprivate func test1() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string: urlString)!

        //创建并发起请求
        request(.get, url).data().subscribe(onNext: {
            data in
            //数据处理
            let str = String(data: data, encoding: String.Encoding.utf8)
            print("返回的数据是：", str ?? "")
        }).disposed(by: disposeB)
    }

    fileprivate func test2() {
        //创建URL对象
        let urlString = "https://www.douban.com/jxxxxxxxx/app/radio/channels"
        let url = URL(string: urlString)!

        //创建并发起请求
        request(.get, url).data().subscribe(onNext: {
            data in
            //数据处理
            let str = String(data: data, encoding: String.Encoding.utf8)
            print("返回的数据是：", str ?? "")
        }, onError: { error in
            print("请求失败！错误原因：", error)
        }).disposed(by: disposeB)
    }

    fileprivate func test3() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string: urlString)!

        //创建并发起请求
        requestData(.get, url).subscribe(onNext: {
            response, data in
            //数据处理
            let str = String(data: data, encoding: String.Encoding.utf8)
            print("返回的数据是：", str ?? "")
        }).disposed(by: disposeB)
    }
}

extension JSViewController45 {
    fileprivate func test4() {
        //创建URL对象
        let urlString = "https://www.douban.com/jxxxxxxx/app/radio/channels"
        let url = URL(string: urlString)!

        //创建并发起请求
        requestData(.get, url).subscribe(onNext: { response, data in
            //判断响应结果状态码
            if 200..<300 ~= response.statusCode {
                let str = String(data: data, encoding: String.Encoding.utf8)
                print("请求成功！返回的数据是：", str ?? "")
            } else {
                print("请求失败！")
            }
        }).disposed(by: disposeB)
    }

    fileprivate func test5() {

        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string: urlString)!

        //创建并发起请求
        request(.get, url).responseString().subscribe(onNext: {
            response, data in
            //数据处理
            print("返回的数据是：", data)
        }).disposed(by: disposeB)
    }

    fileprivate func test6() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string: urlString)!

        //创建并发起请求
        requestString(.get, url).subscribe(onNext: {
            response, data in
            //数据处理
            print("返回的数据是：", data)
        }).disposed(by: disposeB)
    }
}

extension JSViewController45 {

    fileprivate func test7() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string: urlString)
        //创建请求对象
        let request = URLRequest(url: url!)

        navigationBar.setTitles("开始", "取消")
        //发起请求按钮点击
//        let startBtn: UIButton = navigationBar.buttonA
//        let cancelBtn: UIButton = navigationBar.buttonB
//        startBtn.rx.tap.asObservable()
//            .flatMap {
//                request(.get, url).responseString()
//                    .takeUntil(self.cancelBtn.rx.tap) //如果“取消按钮”点击则停止请求
//            }.subscribe(onNext: {
//                response, data in
//                print("请求成功！返回的数据是：", data)
//            }, onError: { error in
//                print("请求失败！错误原因：", error)
//            }).disposed(by: disposeB)
    }
}
